package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sand.dao.IBaseDao;
import com.sand.entity.QuestionEntity;
import com.sand.entity.TRoleuserEntity;
import com.sand.entity.TUserEntity;
import com.sand.entity.WithdrawalEntity;
import com.sand.service.IWithdrawalService;
import com.sand.util.PageModel;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@Service(value = "withdrawalService")
public class WithdrawalServiceImp implements IWithdrawalService {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/2/002 23:58
 */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    //分页查询提现记录
    @Override
    public PageModel<HashMap<String, Object>> queryWithdrawalLimit(int limit, int page, String condition) {
        PageModel<HashMap<String, Object>> pm = new PageModel<HashMap<String, Object>>();

        //计算开始位置
        int begin=(page-1)*limit;
        //创建查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(WithdrawalEntity.class);
        //添加模糊查询条件
        if(condition!=null){
            JSONObject obj = JSON.parseObject(condition);
            if(obj.get("status")!=null && !"".equals(obj.get("status"))){
                dc.add(Restrictions.eq("status",obj.get("status").toString()));
            }
            if(obj.get("userName")!=null && !"".equals(obj.get("userName"))){
                List list = baseDao.queryObject("select t1.userId from UserdetailsEntity t1 where t1.name like '%"+obj.get("userName").toString()+"%'");
                dc.add(Restrictions.in("userId",list));
            }
        }

        List<Long> list1 = baseDao.queryObjectCommon(dc.setProjection(Projections.rowCount()));
        dc.setProjection(null);
        dc.setProjection(Projections.property("rid"));
        dc.addOrder(Order.desc("rid"));
        List list = baseDao.queryObjectLimit(dc, begin, limit);
        long i=list1.get(0);
        int count= (int) i;

        List<HashMap<String, Object>> list2 = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.createTime as createTime,t1.money as money,t1.status as status,t1.account as account," +
                "t2.name as userName) from WithdrawalEntity t1,UserdetailsEntity t2 where t1.userId=t2.userId and t1.rid in (?0)", list);

        pm.setList(list2);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        pm.setPagecount(count);
        return pm;
    }
    //添加提现记录
    @Override
    public boolean addWithdrawal(String data) {
        //创建时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(data!=null){
            WithdrawalEntity with = JSON.parseObject(data, WithdrawalEntity.class);
            with.setCreateTime(timestamp);
            boolean bln = baseDao.addObject(with);
            if(bln){
               return true;
            }
        }
        return false;
    }
}
